//
//  ZUpgradeViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/4/29.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "ZUpgradeViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "NewLineListCell.h"
#import "PMCalendar.h"
#import "ZupgradeCell.h"
#import "LineView.h"
#import "DownLineTableViewCell.h"
#import "YoungAllData.h"
#import "ZiDonBuDanView.h"
#import "GMDCircleLoader.h"
@interface ZUpgradeViewController ()<UITableViewDataSource,UITableViewDelegate,PMCalendarControllerDelegate,UIScrollViewDelegate,YoungDelegate>
{
    UIView *alertView;
    UIView *views;
    UITableView *zTableView;
    UIScrollView *zScrollView;
    
    NSString *zRealName;
    NSString *zRealLevel;
    UILabel *_rankLabela;
}
@property(nonatomic,retain)PMCalendarController *pmCC;
@property (nonatomic, retain) UIButton *dateBtn;

@property(nonatomic, retain)NSString *dateNow;
@property(nonatomic,retain)NSMutableDictionary *zDictionaryData;

@property(nonatomic,retain)NSMutableArray *zArrayData;
@end

@implementation ZUpgradeViewController
@synthesize pmCC,dateBtn,dateNow,zArrayData,zDictionaryData,AgentIDStr;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navRightBtn.hidden = YES;
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate= self;
    [data gainChildrenDetails:AgentIDStr];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addIjia) name:@"addIjia" object:nil];
    
    navTitle.text = @"下线订单";
    //获取现在的日期
    [self strtime];
    
    dateBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
    if (SCREEN_WIDTH == 320) {
        dateBtn.frame = CGRectMake(SCREEN_WIDTH-80,29.5, 70,25);
    }else if (SCREEN_WIDTH == 375){
        dateBtn.frame = CGRectMake(SCREEN_WIDTH-80,35, 70,25);
    }else{
        dateBtn.frame = CGRectMake(SCREEN_WIDTH-80,48.9, 70,25);
    }
    dateBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    dateBtn.layer.borderWidth = 0.5;
    //[self.dateBtn setBackgroundImage:[UIImage imageNamed:@"applySelect"] forState:UIControlStateNormal];
    [dateBtn setTintColor:[UIColor whiteColor]];
    [dateBtn setTitle:dateNow forState:UIControlStateNormal];
    [dateBtn addTarget:self action:@selector(dateBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    //[self.view  addSubview:dateBtn];
}
#pragma mark - 微商下线明细回调
-(void)childrenDetails:(NSDictionary *)sign{
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    zDictionaryData  = [[NSMutableDictionary alloc]initWithDictionary:[sign objectForKey:@"data"]];
    [self createTableView:[[sign objectForKey:@"data"] objectForKey:@"LevelID"]
             andLevelName:[[sign objectForKey:@"data"] objectForKey:@"LevelName"]
          andDiscountRate:[[sign objectForKey:@"data"] objectForKey:@"DiscountRate"]
          andMobileNumber:[[sign objectForKey:@"data"] objectForKey:@"MobileNumber"]
               andAddTime:[[sign objectForKey:@"data"] objectForKey:@"AddTime"]
            andSellAmount:[[sign objectForKey:@"data"] objectForKey:@"SellAmount"]
           andFatherAgent:[[sign objectForKey:@"data"] objectForKey:@"FatherAgent"]
          andFatherMobile:[[sign objectForKey:@"data"] objectForKey:@"FatherMobile"]
       andMonthSellAmount:[[sign objectForKey:@"data"] objectForKey:@"MonthSellAmount"]
              andRealName:[[sign objectForKey:@"data"] objectForKey:@"RealName"]
     ];
    zRealLevel = [[sign objectForKey:@"data"] objectForKey:@"ParentLevelName"];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];

    }
}
#pragma mark - 微商下线明细回调失败
-(void)requistFail{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
#pragma mark -点击日期
- (void)dateBtnAction:(UIButton *)btn
{
    NSLog(@"日期");
    self.pmCC = [[PMCalendarController alloc] init];
    pmCC.delegate = self;
    pmCC.mondayFirstDayOfWeek = YES;
    
    [pmCC presentCalendarFromView:btn
         permittedArrowDirections:PMCalendarArrowDirectionAny
                         animated:YES];
    
    [self calendarController:pmCC didChangePeriod:self.pmCC.period];
    
    
}
//获取当月天数
-(NSInteger)gainNumber{
    NSCalendar *calendar = [NSCalendar currentCalendar];
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSDate *date = [dateFormatter dateFromString:@"2015-02-04 16:01:03"];
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];
    
    NSUInteger numberOfDaysInMonth = range.length;
    return  numberOfDaysInMonth;
}
static int i = 0;
- (void)calendarController:(PMCalendarController *)calendarController didChangePeriod:(PMPeriod *)newPeriod
{
    self.dateBtn.titleLabel.text = [NSString stringWithFormat:@"%@"
                                    , [newPeriod.startDate dateStringWithFormat:@"yyyy-MM"]];
    
    dateNow =[NSString stringWithFormat:@"%@"
              , [newPeriod.startDate dateStringWithFormat:@"yyyy-MM"]];
    if (i%2!=0) {
        [self.pmCC dismissCalendarAnimated:YES];
        
        //        AccountHandleData *data = [[AccountHandleData alloc] init];
        //        data.delegate = self;
        //        [data topUPRecords:single.buyerModel.user_id andTime:dateNow];
        
        
    }
    
    i++;
    
    
}
#pragma mark - addI
-(void)addIjia{
    i++;
}
#pragma mark - 返回
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma -mark 创建tableview
- (void)createTableView:(NSString *)LevelID andLevelName:(NSString *)LevelName andDiscountRate:(NSString *)DiscountRate andMobileNumber:(NSString *)MobileNumber andAddTime:(NSString *)AddTime andSellAmount:(NSString *)SellAmount andFatherAgent:(NSString *)FatherAgent andFatherMobile:(NSString *)FatherMobile andMonthSellAmount:(NSString *)MonthSellAmount andRealName:(NSString *)RealName{
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, navView.bottom, SCREEN_WIDTH, 160)];
    topView.backgroundColor = [UIColor whiteColor];
    topView.userInteractionEnabled = YES;
    [self.view addSubview:topView];
    
    UILabel *namea = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 140, 20)];
    namea.text = [NSString stringWithFormat:@"%@",RealName];
    namea.font = [UIFont systemFontOfSize:12];
    namea.textAlignment = NSTextAlignmentLeft;
    [topView addSubview:namea];
    
    zRealName = RealName;
    //等级
    
    _rankLabela = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100-60, 5, 140, 20)];
    _rankLabela.text = [NSString stringWithFormat:@"等级:%@",LevelName];
    _rankLabela.font = [UIFont systemFontOfSize:12];
    _rankLabela.textAlignment = NSTextAlignmentRight;
    [topView addSubview:_rankLabela];

    
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(20, namea.bottom+5, 140, 20)];
    name.text = [NSString stringWithFormat:@"联系方式:%@",MobileNumber];
    name.font = [UIFont systemFontOfSize:12];
    name.textAlignment = NSTextAlignmentLeft;
    [topView addSubview:name];
    
    //等级
    
    UILabel *rankLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100-60, namea.bottom + 5, 140, 20)];
    rankLabel.text = [NSString stringWithFormat:@"注册时间:%@",AddTime];
    rankLabel.font = [UIFont systemFontOfSize:12];
    rankLabel.textAlignment = NSTextAlignmentRight;
    [topView addSubview:rankLabel];
    
    UILabel *name1 = [[UILabel alloc] initWithFrame:CGRectMake(20, name.bottom+5, 140, 20)];
    name1.text = [NSString stringWithFormat:@"总销售额:%@",SellAmount];
    name1.font = [UIFont systemFontOfSize:12];
    name1.textAlignment = NSTextAlignmentLeft;
    [topView addSubview:name1];
    
    
    UILabel *name2 = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100-60, rankLabel.bottom+5, 140, 20)];
    name2.text = [NSString stringWithFormat:@"30天内销售额:%@",MonthSellAmount];
    name2.font = [UIFont systemFontOfSize:12];
    name2.textAlignment = NSTextAlignmentRight;
    [topView addSubview:name2];
    
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(20, name2.bottom+5, SCREEN_WIDTH-40, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [topView addSubview:line];
    
    
    UILabel *name3 = [[UILabel alloc] initWithFrame:CGRectMake(20, name1.bottom+10, 140, 20)];
    name3.text = [NSString stringWithFormat:@"上线:%@",FatherAgent];
    name3.font = [UIFont systemFontOfSize:12];
    name3.textAlignment = NSTextAlignmentLeft;
    [topView addSubview:name3];
    
    
    UILabel *name4 = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100-60,name2.bottom+10, 140, 20)];
    name4.text = [NSString stringWithFormat:@"联系方式:%@",FatherMobile];
    name4.font = [UIFont systemFontOfSize:12];
    name4.textAlignment = NSTextAlignmentRight;
    [topView addSubview:name4];
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(20, name4.bottom+5, SCREEN_WIDTH-40, 1)];
    line1.backgroundColor = [UIColor lightGrayColor];
    [topView addSubview:line1];
    
    //for (int i = 0; i < 2; i++) {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    if (SCREEN_WIDTH==320) {
        //btn.frame = CGRectMake(20+i*150, name4.bottom+15, 130, 24);
        
        btn.frame = CGRectMake((SCREEN_WIDTH-130)/2, name3.bottom+15, 130, 24);
    }else if (SCREEN_WIDTH == 375){
        //btn.frame = CGRectMake(20+i*182, name4.bottom+15, 152, 29);
        
    }else{
        // btn.frame = CGRectMake(20+i*199, name4.bottom+15, 169, 32);
        
    }
    
    // switch (i) {
    //   case 0:
    [btn setBackgroundImage:[UIImage imageNamed:@"下线订单_03"] forState:UIControlStateNormal];
    //    break;
    
    //  default:
    //    [btn setBackgroundImage:[UIImage imageNamed:@"下线订单_05"] forState:UIControlStateNormal];
    //     break;
    //    }
    btn.tag = 0;
    [btn addTarget:self action:@selector(clickUp:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor whiteColor];
    [topView addSubview:btn];
    // }
    
    UIButton *belowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    belowBtn.frame = CGRectMake(0,SCREEN_HEIGHT-(0.15625*SCREEN_WIDTH) , SCREEN_WIDTH, 0.15625*SCREEN_WIDTH);
    [belowBtn setBackgroundImage:[UIImage imageNamed:@"信息_06"] forState:UIControlStateNormal];
    [belowBtn addTarget:self action:@selector(backGo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:belowBtn];
    
    //创建tableview
    zTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, topView.bottom+10, SCREEN_WIDTH, SCREEN_HEIGHT-navView.frame.size.height-(0.15625*SCREEN_WIDTH)-topView.frame.size.height-10)];
    zTableView.dataSource = self;
    zTableView.delegate = self;
    [self.view addSubview:zTableView];
    zTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self setExtraCellLineHidden:zTableView];
    
    
}
#pragma mark - UitableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == zTableView) {
        return 50;
    }else{
        return 300;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == zTableView) {
        return [[zDictionaryData objectForKey:@"OrderInfo"] count];
    }else{
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *iden = @"shoppingCell";
    if (tableView == zTableView) {
        DownLineTableViewCell *cell = [[DownLineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        
        NSArray *arr = [zDictionaryData objectForKey:@"OrderInfo"];
        NSDictionary *dataDic = arr[indexPath.row];
        cell.ziDonBuDanView.listNium.text = [NSString stringWithFormat:@"订单号:%@",dataDic[@"MasterOrderID"]];
        cell.ziDonBuDanView.xiaDanTime.text = [NSString stringWithFormat:@"下单时间:%@",dataDic[@"AddTime"]];
        cell.ziDonBuDanView.nowPrice.text = [NSString stringWithFormat:@"(%@)",dataDic[@"TotalAmount"]];
        cell.ziDonBuDanView.oldPrice.text = [NSString stringWithFormat:@"¥%@",dataDic[@"DiscountAmount"]];
        //cell.zProfit.text = @"盈利：￥99";
        cell.ziDonBuDanView.profitLabel.hidden = YES;
        cell.ziDonBuDanView.profitNumber.hidden = YES;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        
        return cell;
    }else{
        NewLineListCell *cell =  [tableView dequeueReusableCellWithIdentifier:iden];
        if (cell == nil) {
            cell = [[NewLineListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        }
        cell.lineView.queRenBut.hidden = YES;
        cell.lineView.state.hidden = NO;
        cell.lineView.stateLabel.hidden = NO;
        return cell;
    }
}


#pragma mark - 隐藏多余的分割线
-(void)setExtraCellLineHidden: (UITableView *)tableView

{
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
}
#pragma mark - 返回
-(void)backGo{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 升级降级
-(void)clickUp:(UIButton *)sender{
    int j = 0;
    if (sender.tag == 0) {
        //第一种情况
        if (j==0) {
            views = [[UIView alloc]init];
            views.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            views.userInteractionEnabled = YES;
            views.backgroundColor = [UIColor blackColor];
            views.alpha = 0.4;
            [self.view addSubview:views];
            
            alertView = [[UIView alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT/2-150, SCREEN_WIDTH-120, 120)];
            alertView.backgroundColor = [UIColor whiteColor];
            alertView.userInteractionEnabled = YES;
            alertView.layer.cornerRadius = 10;//设置那个圆角的有多圆
            [self.view addSubview:alertView];
            
            UILabel *alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, alertView.frame.size.width-40, 40)];
            NSString *ptr = [NSString stringWithFormat:@"确认将%@的微商等级提升至%@?",zRealName,zRealLevel];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:ptr];
           
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,2)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3,zRealName.length)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(3+zRealName.length,8)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(zRealName.length+11,2)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(zRealName.length+13,1)];
            alertLabel.attributedText = str;
            alertLabel.numberOfLines = 0;
            alertLabel.font = [UIFont systemFontOfSize:14];
            alertLabel.textAlignment = NSTextAlignmentCenter;
            [alertView addSubview:alertLabel];
            
            [self createrOffLineUI:CGRectMake(0, alertLabel.bottom+31, alertView.frame.size.width, 0.5)];
            //
            
            UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            leftBtn.frame = CGRectMake(0, alertLabel.bottom+32, alertView.frame.size.width/2, 28);
            //leftBtn.backgroundColor= [ UIColor whiteColor];
            UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:leftBtn.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)];
            CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
            maskLayer1.frame = leftBtn.bounds;
            maskLayer1.path = maskPath1.CGPath;
            leftBtn.layer.mask = maskLayer1;
            [leftBtn addTarget:self action:@selector(clickLeft) forControlEvents:UIControlEventTouchUpInside];
            [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_03"] forState:UIControlStateNormal];
            [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_03"] forState:UIControlStateHighlighted];
            [alertView addSubview:leftBtn];
            
            UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            rightBtn.frame = CGRectMake(alertView.frame.size.width/2, alertLabel.bottom+32, alertView.frame.size.width/2, 28);
            UIBezierPath *maskPath11 = [UIBezierPath bezierPathWithRoundedRect:rightBtn.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
            CAShapeLayer *maskLayer11 = [[CAShapeLayer alloc] init];
            maskLayer11.frame = rightBtn.bounds;
            maskLayer11.path = maskPath11.CGPath;
            rightBtn.layer.mask = maskLayer11;
            [rightBtn addTarget:self action:@selector(cliclRight) forControlEvents:UIControlEventTouchUpInside];
            [rightBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_04"] forState:UIControlStateNormal];
            [rightBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_04"] forState:UIControlStateHighlighted];
            [alertView addSubview:rightBtn];
            
        }else if (j==1){
            
            views = [[UIView alloc]init];
            views.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            views.userInteractionEnabled = YES;
            views.backgroundColor = [UIColor blackColor];
            views.alpha = 0.4;
            [self.view addSubview:views];
            
            alertView = [[UIView alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT/2-150, SCREEN_WIDTH-120, 120)];
            alertView.backgroundColor = [UIColor whiteColor];
            alertView.userInteractionEnabled = YES;
            alertView.layer.cornerRadius = 10;//设置那个圆角的有多圆
            [self.view addSubview:alertView];
            
            UILabel *alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, alertView.frame.size.width-40, 40)];
            alertLabel.text = @"您不能下线提升至您的等级";
            alertLabel.numberOfLines = 0;
            alertLabel.textColor = [UIColor blackColor];
            alertLabel.font = [UIFont systemFontOfSize:14];
            alertLabel.textAlignment = NSTextAlignmentCenter;
            [alertView addSubview:alertLabel];
            
            [self createrOffLineUI:CGRectMake(0, alertLabel.bottom+31, alertView.frame.size.width, 0.5)];
            
            //
            UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            leftBtn.frame = CGRectMake(0, alertLabel.bottom+32, alertView.frame.size.width, 28);
            UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:leftBtn.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
            CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
            maskLayer1.frame = leftBtn.bounds;
            maskLayer1.path = maskPath1.CGPath;
            leftBtn.layer.mask = maskLayer1;
            
            [leftBtn addTarget:self action:@selector(cliclRight) forControlEvents:UIControlEventTouchUpInside];
            [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_07"] forState:UIControlStateNormal];
            [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_07"] forState:UIControlStateHighlighted];
            [alertView addSubview:leftBtn];
            
        }
        
    }else{
        if (j==0) {
            views = [[UIView alloc]init];
            views.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            views.userInteractionEnabled = YES;
            views.backgroundColor = [UIColor blackColor];
            views.alpha = 0.4;
            [self.view addSubview:views];
            
            alertView = [[UIView alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT/2-150, SCREEN_WIDTH-120, 120)];
            alertView.backgroundColor = [UIColor whiteColor];
            alertView.userInteractionEnabled = YES;
            alertView.layer.cornerRadius = 10;//设置那个圆角的有多圆
            [self.view addSubview:alertView];
            
            UILabel *alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, alertView.frame.size.width-40, 40)];
            
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"确认将谷铮的微商等级降低至特约?"];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,2)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3,2)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(5,8)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(13,2)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(15,1)];
            alertLabel.attributedText = str;
            alertLabel.numberOfLines = 0;
            alertLabel.font = [UIFont systemFontOfSize:14];
            alertLabel.textAlignment = NSTextAlignmentCenter;
            [alertView addSubview:alertLabel];
            
            [self createrOffLineUI:CGRectMake(0, alertLabel.bottom+31, alertView.frame.size.width, 0.5)];
            //
            
            UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            leftBtn.frame = CGRectMake(0, alertLabel.bottom+32, alertView.frame.size.width/2, 28);
            //leftBtn.backgroundColor= [ UIColor whiteColor];
            UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:leftBtn.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)];
            CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
            maskLayer1.frame = leftBtn.bounds;
            maskLayer1.path = maskPath1.CGPath;
            leftBtn.layer.mask = maskLayer1;
            [leftBtn addTarget:self action:@selector(clickLeft1) forControlEvents:UIControlEventTouchUpInside];
            [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_03"] forState:UIControlStateNormal];
            [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_03"] forState:UIControlStateHighlighted];
            [alertView addSubview:leftBtn];
            
            UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            rightBtn.frame = CGRectMake(alertView.frame.size.width/2, alertLabel.bottom+32, alertView.frame.size.width/2, 28);
            UIBezierPath *maskPath11 = [UIBezierPath bezierPathWithRoundedRect:rightBtn.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
            CAShapeLayer *maskLayer11 = [[CAShapeLayer alloc] init];
            maskLayer11.frame = rightBtn.bounds;
            maskLayer11.path = maskPath11.CGPath;
            rightBtn.layer.mask = maskLayer11;
            [rightBtn addTarget:self action:@selector(cliclRight) forControlEvents:UIControlEventTouchUpInside];
            [rightBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_04"] forState:UIControlStateNormal];
            [rightBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_04"] forState:UIControlStateHighlighted];
            [alertView addSubview:rightBtn];
            
        }else{
            views = [[UIView alloc]init];
            views.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            views.userInteractionEnabled = YES;
            views.backgroundColor = [UIColor blackColor];
            views.alpha = 0.4;
            [self.view addSubview:views];
            
            alertView = [[UIView alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT/2-150, SCREEN_WIDTH-120, 120)];
            alertView.backgroundColor = [UIColor whiteColor];
            alertView.userInteractionEnabled = YES;
            alertView.layer.cornerRadius = 10;//设置那个圆角的有多圆
            [self.view addSubview:alertView];
            
            UILabel *alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, alertView.frame.size.width-40, 40)];
            alertLabel.text = @"该下线已是最低等级";
            alertLabel.numberOfLines = 0;
            alertLabel.textColor = [UIColor blackColor];
            alertLabel.font = [UIFont systemFontOfSize:14];
            alertLabel.textAlignment = NSTextAlignmentCenter;
            [alertView addSubview:alertLabel];
            
            [self createrOffLineUI:CGRectMake(0, alertLabel.bottom+31, alertView.frame.size.width, 0.5)];
            
            //
            UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            leftBtn.frame = CGRectMake(0, alertLabel.bottom+32, alertView.frame.size.width, 28);
            UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:leftBtn.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
            CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
            maskLayer1.frame = leftBtn.bounds;
            maskLayer1.path = maskPath1.CGPath;
            leftBtn.layer.mask = maskLayer1;
            
            [leftBtn addTarget:self action:@selector(cliclRight) forControlEvents:UIControlEventTouchUpInside];
            [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_07"] forState:UIControlStateNormal];
            [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_07"] forState:UIControlStateHighlighted];
            [alertView addSubview:leftBtn];
            
        }
        
    }
}
#pragma mark - 确定升级
-(void)clickLeft{
    [GMDCircleLoader setOnView:self.view withTitle:@"升级中···" animated:YES];
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate = self;
    [data upgradeNow:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"] andChildAgentID:AgentIDStr];
    
    [alertView removeFromSuperview];
    alertView = nil;
    [views removeFromSuperview];
    views =nil;
    /*
    //确认升级
    //    //接口判断1级微商是否已满
    //    views = [[UIView alloc]init];
    //    views.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    //    views.userInteractionEnabled = YES;
    //    views.backgroundColor = [UIColor blackColor];
    //    views.alpha = 0.4;
    //    [self.view addSubview:views];
    //
    //    alertView = [[UIView alloc]initWithFrame:CGRectMake(60, SCREEN_HEIGHT/2-150, SCREEN_WIDTH-120, 150)];
    //    alertView.backgroundColor = [UIColor whiteColor];
    //    alertView.userInteractionEnabled = YES;
    //    alertView.layer.cornerRadius = 10;//设置那个圆角的有多圆
    //    [self.view addSubview:alertView];
    //
    //    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,  alertView.frame.size.width, 30)];
    //    topView.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    //    UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:topView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
    //    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    //    maskLayer1.frame = topView.bounds;
    //    maskLayer1.path = maskPath1.CGPath;
    //    topView.layer.mask = maskLayer1;
    //    [alertView addSubview:topView];
    //
    //    UILabel *alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, alertView.frame.size.width, 30)];
    //    alertLabel.text = @"请选择替换的1级微商";
    //    alertLabel.textColor = [UIColor redColor];
    //    alertLabel.font = [UIFont systemFontOfSize:12];
    //    alertLabel.textAlignment = NSTextAlignmentCenter;
    //    [topView addSubview:alertLabel];
    //
    //
    //    zScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, topView.bottom, alertView.frame.size.width, 90)];
    //    zScrollView.bounces = NO;
    //    zScrollView.contentSize = CGSizeMake(alertView.frame.size.width, 120);
    //    [alertView addSubview:zScrollView];
    //
    //    for (int i = 0;i < 4 ; i++) {
    //        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0+i*30, alertView.frame.size.width, 30)];
    //        titleLabel.textColor = [UIColor grayColor];
    //        titleLabel.text = @"1111";
    //        titleLabel.tag = i+50;
    //        titleLabel.textAlignment = NSTextAlignmentCenter;
    //        titleLabel.font = [UIFont systemFontOfSize:14];
    //        [zScrollView addSubview:titleLabel];
    //
    //
    //        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    //        btn1.frame = CGRectMake(alertView.frame.size.width-27, 6.5+30*i, 17, 17  );
    //        btn1.backgroundColor = [UIColor clearColor];
    //        btn1.tag = i+40;
    //        //[btn setBackgroundImage:[UIImage imageNamed:@"下线订单---弹窗_07"] forState:UIControlStateNormal];
    //        //[btn1 addTarget:self action:@selector(clickkk1:) forControlEvents:UIControlEventTouchUpInside];
    //        [zScrollView addSubview:btn1];
    //
    //        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //        btn.frame = CGRectMake(0, 0+i*30, alertView.frame.size.width, 30);
    //        btn.backgroundColor = [UIColor clearColor];
    //        btn.tag = i+30;
    //        //[btn setBackgroundImage:[UIImage imageNamed:@"下线订单---弹窗_07"] forState:UIControlStateNormal];
    //        [btn addTarget:self action:@selector(clickkk:) forControlEvents:UIControlEventTouchUpInside];
    //        [zScrollView addSubview:btn];
    //
    //
    //        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //        leftBtn.frame = CGRectMake(0, zScrollView.bottom+2, alertView.frame.size.width/2, 28);
    //        //leftBtn.backgroundColor= [ UIColor whiteColor];
    //        UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:leftBtn.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)];
    //        CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    //        maskLayer1.frame = leftBtn.bounds;
    //        maskLayer1.path = maskPath1.CGPath;
    //        leftBtn.layer.mask = maskLayer1;
    //        [leftBtn addTarget:self action:@selector(clickLeft1) forControlEvents:UIControlEventTouchUpInside];
    //        [leftBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_03"] forState:UIControlStateNormal];
    //        [alertView addSubview:leftBtn];
    //
    //        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //        rightBtn.frame = CGRectMake(alertView.frame.size.width/2,zScrollView.bottom+2, alertView.frame.size.width/2, 28);
    //        UIBezierPath *maskPath11 = [UIBezierPath bezierPathWithRoundedRect:rightBtn.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    //        CAShapeLayer *maskLayer11 = [[CAShapeLayer alloc] init];
    //        maskLayer11.frame = rightBtn.bounds;
    //        maskLayer11.path = maskPath11.CGPath;
    //        rightBtn.layer.mask = maskLayer11;
    //        [rightBtn addTarget:self action:@selector(cliclRight) forControlEvents:UIControlEventTouchUpInside];
    //        [rightBtn setImage:[UIImage imageNamed:@"下线订单---弹窗_04"] forState:UIControlStateNormal];
    //        [alertView addSubview:rightBtn];
    //
    //
    //    }
     */
}
-(void)upgradeNow:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
        _rankLabela.text = [NSString stringWithFormat:@"等级:%@",zRealLevel];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"升级成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }

}
-(void)clickkk:(UIButton *)sender{
    NSLog(@"%ld",(long)sender.tag);
    for (int i =0; i < 4; i++) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:(i+40)];
        //btn.frame = CGRectMake(0, 0+(i+30)*30,alertView.frame.size.width, 30);
        btn.backgroundColor = [UIColor clearColor];
        [btn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        
        UILabel *label = (UILabel *)[self.view viewWithTag:(i+50)];
        label.textColor = [UIColor grayColor];
    }
    UILabel *label = (UILabel *)[self.view viewWithTag:sender.tag+20];
    label.textColor = [UIColor blackColor];
    UIButton *btn = (UIButton *)[self.view viewWithTag:sender.tag+10];
    [btn setBackgroundImage:[UIImage imageNamed:@"下线订单---弹窗_10"] forState:UIControlStateNormal];
    
}
-(void)dismissPicture:(UIButton *)sender{
    UIButton *btn = (UIButton *)[self.view viewWithTag:sender.tag];
    btn.frame = CGRectMake(0, 0+(sender.tag-30)*30,alertView.frame.size.width, 30);
    btn.backgroundColor = [UIColor clearColor];
    [btn setBackgroundImage:[UIImage imageNamed:@"下线订单---弹窗_07"] forState:UIControlStateNormal];
    
}
-(void)clickLeft1{
    [alertView removeFromSuperview];
    alertView = nil;
    [views removeFromSuperview];
    views =nil;
    
}
#pragma mark - clickRight
-(void)cliclRight{
    [alertView removeFromSuperview];
    alertView = nil;
    [views removeFromSuperview];
    views =nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - 获取当前时间
-(void)strtime{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    now=[NSDate date];
    comps = [calendar components:unitFlags fromDate:now];
    NSInteger year = [comps year];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    NSString *monthStr ;
    NSString *dayStr ;
    if (month < 10 || day < 10) {
        if (month <10) {
            monthStr = [NSString stringWithFormat:@"0%ld",(long)month];
        }else{
            monthStr = [NSString stringWithFormat:@"%ld",(long)month];
        }
        if(day < 10){
            dayStr = [NSString stringWithFormat:@"0%ld",(long)day];
        }else{
            dayStr = [NSString stringWithFormat:@"%ld",(long)day];
        }
    }else{
        monthStr = [NSString stringWithFormat:@"%ld",(long)month];
        dayStr = [NSString stringWithFormat:@"%ld",(long)day];
    }
    
    //dateNow = [NSString stringWithFormat:@"%ld-%@-%@",(long)year,monthStr,dayStr];
    dateNow = [NSString stringWithFormat:@"%ld-%@",(long)year,monthStr];
}
-(void)createrOffLineUI:(CGRect)rect{
    UIView *lines = [[UIView alloc] initWithFrame:rect];
    lines.backgroundColor = [UIColor colorWithRed:179/255.0 green:179/255.0 blue:179/255.0 alpha:1];
    [alertView addSubview:lines];
}
@end
